package com.pxmeta.verify.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.pxmeta.common.constant.Status;
import com.pxmeta.common.utils.HttpUtils;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserIdentify;
import com.pxmeta.user.entity.UserStudent;
import com.pxmeta.user.mapper.UserStudentMapper;
import com.pxmeta.user.service.IUserIdentifyService;
import com.pxmeta.user.service.IUserService;
import com.pxmeta.user.service.IUserStudentService;
import com.pxmeta.verify.entity.StuInfo;
import com.pxmeta.verify.entity.UserReal;
import com.pxmeta.verify.mapper.UserRealMapper;
import com.pxmeta.verify.service.IUserRealService;
import lombok.RequiredArgsConstructor;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-07-03
 */
@Service
@RequiredArgsConstructor
public class UserRealServiceImpl extends ServiceImpl<UserRealMapper, UserReal> implements IUserRealService {

    private final IUserService userService;

    @Override
    public boolean verify(Long userId, String realName, String idCard) throws IOException {
        String url = "https://eid.shumaidata.com/eid/check";
        String appCode = "9233aaa4ea384f0795b4652c9a48eea8";
        Map<String, String> querys = Maps.newHashMap();
        querys.put("idcard", idCard);
        querys.put("name", realName);
        String str = postForm(appCode, url, querys);
        System.out.println(str);
        JSONObject obj = JSONObject.parseObject(str);
        Integer code = obj.getInteger("code");
        JSONObject result = obj.getJSONObject("result");
        Integer res = result.getInteger("res");

        UserReal userReal = new UserReal();
        userReal.setUserId(userId);
        userReal.setRealName(realName);
        userReal.setIdCard(idCard);
        userReal.setAddress(result.getString("address"));
        userReal.setBirthday(result.getString("birthday"));
        userReal.setStatus(code == 0&&res ==1? Status.SUCCESS.getValue() :Status.FAIL.getValue());
        this.saveOrUpdate(userReal);
        if (code == 0 && res == 1){
            User user = userService.getById(userId);
            user.setIsAuthIdentity(Status.SUCCESS.getValue());
            userService.updateById(user);
        }
        return true;
    }


    public static String postForm(String appCode, String url, Map<String, String> params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody.Builder formbuilder = new FormBody.Builder();
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            formbuilder.add(key, params.get(key));
        }
        FormBody body = formbuilder.build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(body).build();
        Response response = client.newCall(request).execute();
        System.out.println("返回状态码" + response.code() + ",message:" + response.message());
        String result = response.body().string();
        return result;
    }
}
